# -*- coding: UTF-8 -*-
#coding=utf-8
import os

#遍历目录
def travelDir(dirName):
    i = 0
    fileAllNames = []
    if os.path.isdir(dirName):  
        for root, dirs, files in os.walk(dirName):
            for filename in files:
                fileAllName=root+'\\'+filename
                fileAllNames.insert(i,fileAllName)
    return fileAllNames

#fileNameExtArr是允许通过的扩展名字符串数组
def filter(fileAllNamesArr, fileNameExtArr):
    fileAllNames = []
    i = 0
    for fileAllName in fileAllNamesArr:
        for ext in fileNameExtArr:
            if(os.path.splitext(fileAllName)[-1] == ext):
                fileAllNames.insert(i,fileAllName)
                ++i
                break
    return fileAllNames
#每一个文件进行计数
def count(fileAllName):
    with open(fileAllName) as f:
        text=f.read()
        return length=len(text.splitlines())
if __name__ == '__main__':
    fileNameExtArr = ['.html', '.css', '.js', '.py', '.txt']
    fileAllNames = travelDir('./')
    fileAllNames = filter(fileAllNames, fileNameExtArr)
    n = 0
    for fileAllName in fileAllNames:
        n = n + count(fileAllName)
    print 'code lines is:%d'%n
    
